Shader "URP-YoFi/Character/Character-Emote" 
{
	Properties
	{
		_MainColorTint("Main Color Tint", color) = (1,1,1,1)
//		_AmbientEffectFactor("Ambient Effect Factor", Range(0,1)) = 1
//		
//		_DarkColor("Dark Color", color) = (0.5,0.5,0.5,1)
//		_LambertMin ("Lambert Min", Range(0,1)) = 0
//		_LambertMax ("Lambert Max", Range(0,1)) = 1
//		_FresnelMin ("Fresnel Min", Range(0,1)) = 0
//		_FresnelMax ("Fresnel Max", Range(0,1)) = 1
//		[HDR]_FresnelColor("Fresnel Color", color) = (0.4,0.4,0.4,1)
//        _SupLightDir("SupLight Dir", Vector) = (-0.891, 0.454, 0.00,0)


		_MainTex ("MainTex", 2D) = "white" {}
		_EmoteNumber("EmoteNumber",Range(0,7)) = 0
//		_DarkTex ("DarkTex", 2D) = "black" {}
//		_OutlineWidth("_OutlineWidth", float) = 0.003
//        _OutlineTint("_OutlineTint", color) = (0,0,0,1)
	}

	SubShader 
	{
		Tags
		{
			"RenderType" = "Transparent"
            "Queue" = "Transparent"
		}
		
		HLSLINCLUDE
		
		#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
		#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
		#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
		
		TEXTURE2D(_MainTex);
		//SAMPLER(sampler_MainTex);
		TEXTURE2D(_DarkTex);
		TEXTURE2D(_RMATex);
		SAMPLER(sampler_RMATex);
        TEXTURE2D (_NormalTex);
        SAMPLER(sampler_NormalTex);

		SAMPLER(sampler_linear_repeat);
        SAMPLER(sampler_linear_clamp);


		float3 _CharaMainColorTint;
		float3 _CharaDarkColorTint;
		
		CBUFFER_START(UnityPerMaterial)
		float3 _MainColorTint;
		float _AmbientEffectFactor;

		float4 _DarkColor;
		float _LambertMin;
		float _LambertMax;
		float _FresnelMin;
		float _FresnelMax;
		float3 _SupLightDir;
		float3 _FresnelColor;

		float4 _NormalTex_ST;
        //float _BakedLightIntensity;
        float _NormalScale;
		float _Metallic;
		float _Roughness;

		float _OutlineWidth;
        float3 _OutlineTint;


		float _EmoteNumber;
		CBUFFER_END
		        
		ENDHLSL

		Pass
		{
			
			Blend SrcAlpha OneMinusSrcAlpha, One OneMinusSrcAlpha
            ZWrite Off
            ZTest  On
            Offset -1 , 0
            
			HLSLPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			// #pragma multi_compile _ _MAIN_LIGHT_SHADOWS 
			// #pragma multi_compile _ _SHADOWS_SOFT
			// #pragma multi_compile _ LIGHTMAP_ON
			//#pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
            //#pragma multi_compile _ SHADOWS_SHADOWMASK
			//#pragma multi_compile _ CALCULATE_BAKED_SHADOWS
			
			

			struct Attributes
			{
				float4 positionOS : POSITION;
				float3 normalOS     : NORMAL;
				float2 uv : TEXCOORD0;
				float2 uv2 : TEXCOORD1;
                float4 tangentOS : TANGENT;

			};

			struct Varyings
			{
				float4 positionHCS : SV_POSITION;
				float2 uv : TEXCOORD0;
				float2 uv2 : TEXCOORD1;
				float3 positionWS : TEXCOORD2;
				float3 normalWS  : TEXCOORD3;
                float3 tangentWS :TEXCOORD4;
                float3 BtangentWS : TEXCOORD5;

                float4 screenPos : TEXCOORD6;

				DECLARE_LIGHTMAP_OR_SH(lightmapUV, vertexSH,7);

			};

			Varyings vert (Attributes IN)
			{
				Varyings OUT;
				OUT.uv = IN.uv;
				OUT.uv2 = IN.uv2;
				OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
				OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
                OUT.normalWS = TransformObjectToWorldNormal(IN.normalOS).xyz;
                OUT.tangentWS = TransformObjectToWorldDir(IN.tangentOS.xyz);
                OUT.BtangentWS = cross(OUT.normalWS.xyz,OUT.tangentWS.xyz) * IN.tangentOS.w * unity_WorldTransformParams.w;

				OUT.screenPos = ComputeScreenPos(OUT.positionHCS);

                OUTPUT_LIGHTMAP_UV(IN.uv2, unity_LightmapST, OUT.lightmapUV);
                OUTPUT_SH(OUT.normalWS, OUT.vertexSH);
				return OUT;
			}

			half4 frag (Varyings IN) : SV_Target
			{

				float3 positionWS = IN.positionWS;
                float3 viewDirectionWS = normalize(_WorldSpaceCameraPos.xyz - positionWS);
                float3 normalWS = SafeNormalize(IN.normalWS);
                float3 tangentWS = SafeNormalize(IN.tangentWS);
                float3 bitTangentWS = SafeNormalize(IN.BtangentWS);

                float3 testValue = 0;

				float id = round(_EmoteNumber);
				float columnRef = 2.0f;
				float rowRef = 4.0f;
				float row = floor( id / columnRef);
				float column = fmod(id , rowRef);
				
				float2 faceUV = IN.uv.xy / float2(columnRef,rowRef) + float2(column,row) / float2(columnRef,rowRef);
				
				half4 mainTex = SAMPLE_TEXTURE2D(_MainTex, sampler_linear_repeat, faceUV);
				half4 darkTex = SAMPLE_TEXTURE2D(_DarkTex, sampler_linear_repeat, IN.uv);
                half3 mainColor = mainTex.rgb * _MainColorTint;
                
                             
                Light mainLight =  GetMainLight();//获取主光源
                half3 ambient = mainLight.color.rgb + _AmbientEffectFactor * _GlossyEnvironmentColor.rgb;
             
                half3 finalColor = mainColor  *  ambient;

				float3x3 TBN = float3x3(tangentWS, bitTangentWS, normalWS);
                float3 normalTex  = UnpackNormalScale(SAMPLE_TEXTURE2D(_NormalTex, sampler_linear_repeat, IN.uv),_NormalScale);
                normalWS = normalize(mul(normalTex, TBN));

				float ndotv = dot( normalWS,viewDirectionWS )*2;
                float3 normaltemp =  normalWS * ndotv - viewDirectionWS;
                float3 normalView = mul((float3x3)GetWorldToViewMatrix(), normaltemp).xyz;
                normalView = normalize(normalView);
                //float3 normalView = TransformWorldToViewDir(normalWS,true);
                float3 normalView01 = normalView * 0.5 + 0.5;

				
                float lambert = dot(normalWS,mainLight.direction);
                lambert = lambert * 0.5 + 0.5;
				
				float ramp = smoothstep(  0.48,0.5 ,saturate(lambert * mainLight.shadowAttenuation) );

				finalColor = lerp(mainTex.rgb ,mainTex.rgb, ramp );
				// finalColor = mainTex.rgb;
				
				// float fresnel = smoothstep(  _FresnelMin,_FresnelMax ,normalView01.z );
				// float supLightMask = dot( normalView , normalize(_SupLightDir.xyz));
				// supLightMask = clamp(supLightMask, 0, 1);
    //             fresnel = min(supLightMask,fresnel);
				// float3 fresnelColor = fresnel * _FresnelColor.rgb;
				
				//finalColor += fresnelColor;

				
				return half4(finalColor,mainTex.a);
			}
			ENDHLSL 
		}
		
	}
}